<?php

class ContentEditor extends TTemplateControl
{
	public $content;
	
	public function onLoad()
	{		
		$manager = $this->Application->Modules['contents'];
		$this->content = $manager->getContentByName($this->getName());

		if(!$this->Page->getIsPostback())
			$this->readData();
	}
	
	public function readData()
	{
		if($this->content)
			$this->content_text->Text = $this->content->getText();
	}
	
	public function saveChangesButtonClicked($sender, $param)
	{
		$manager = $this->Application->Modules['contents'];

		// if doen't exist add new
		if(!$manager->hasContent($this->getName()))
			$manager->addContent($this->getName(), $this->content_text->Text);
		// else update already existing
		else
		{
			$this->content->setText($this->content_text->Text);
			$manager->updateContentByContentRecord($this->content);
		}
		$this->Response->reload();
	}
	
	public function cancelChangesButtonClicked($sender, $param)
	{
		$this->Response->reload();
	}
	
	public function editVisibleButtonClicked($sender, $param)
	{
		$this->setEditorVisible(!$this->getEditorVisible());
	}
	
	public function setEditorVisible($visible)
	{
		$this->Page->setViewState('editor_visible', $visible);
	}
	
	public function getEditorVisible()
	{
		return $this->Page->getViewState('editor_visible', false);
	}
	
	public function setName($name)
	{
		$this->Page->setViewState('content_name', $name);
	}
	
	public function getName()
	{
		return $this->Page->getViewState('content_name', $this->Page->getPagePath());
	}
	
}

?>